package com.yingsheng.bi.hks.repository.dao.service;

import com.yingsheng.bi.hks.basic.consts.MsgDict;
import com.yingsheng.bi.hks.basic.exception.ServiceRuntimeException;
import com.yingsheng.bi.hks.dto.goods.GoodsQryReq;
import com.yingsheng.bi.hks.repository.dao.mapper.*;
import com.yingsheng.bi.hks.repository.dao.po.*;
import com.yingsheng.bi.hks.repository.dao.po.ext.GoodsOrder;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Goods表的数据库操作服务
 * 要求说明：
 * 1.一般情况下,要求参数不超过5个时方法入参逐个声明
 * 2.一般情况下,要求条件不超过3个时要在方法名上体现
 * 3.最好对方法入参做一定校验,尤其是对条件参数至少要做非空校验
 *
 * @author 83686
 */
@Repository
public class GoodsDaoService implements DaoService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private GoodsSpecMapper goodsSpecMapper;

    @Autowired
    private SpecItemMapper specItemMapper;

    @Autowired
    private SpecValueMapper specValueMapper;

    @Autowired
    private GoodsExtMapper goodsExtMapper;

    public boolean insert(Goods goods) {
        int result = goodsMapper.insert(goods);
        return result == 1;
    }

    public List<Goods> qry(GoodsQryReq req,String state) {
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isNullOrEmpty(req.getCategoryId())) {
            criteria.andCategoryIdEqualTo(req.getCategoryId());
        }
        if (!StringUtil.isNullOrEmpty(req.getName())) {
            criteria.andNameLike("%" + req.getName() + "%");
        }
        criteria.andStateEqualTo(state);
        List<Goods> goodsList = goodsMapper.selectByExample(example);

        return goodsList;
    }

    public Goods selectGoodsById(@NotBlank String goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    public Goods selectGoodsByIdAndCheckNoNull(@NotBlank String goodsId) {
        Goods goods = goodsMapper.selectByPrimaryKey(goodsId);
        if (goods == null) {
            throw new ServiceRuntimeException(MsgDict.DAO_PARAMETER_ERROR);
        }
        return goods;
    }

    public void goodsSpecAdd(List<GoodsSpec> goodsSpecs, List<SpecItem> specItems, List<SpecValue> specValues) {
        goodsSpecs.forEach(t -> {
            goodsSpecMapper.insert(t);
        });

        specItems.forEach(t -> {
            specItemMapper.insert(t);
        });

        specValues.forEach(t -> {
            specValueMapper.insert(t);
        });
    }

    public List<GoodsSpec> selectSpecItemByGoodsId(String goodsId) {
        return goodsExtMapper.selectGoodsSpecByGoodsId(goodsId);
    }

    public SpecItem selectSpecItemById(String id) {
        return specItemMapper.selectByPrimaryKey(id);
    }

    public List<SpecValue> selectSpecValuesByItemId(String specId){
        SpecValueExample example = new SpecValueExample();
        example.createCriteria().andSpecIdEqualTo(specId);
        return specValueMapper.selectByExample(example);
    }

    public List<GoodsOrder> selectGoodsOrderByUserId(String userId,String orderState){
        return goodsExtMapper.selectGoodsOrderByUserId(userId, orderState);
    }
}
